<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="mini"
      :inline="true"
      v-show="showSearch"
      label-width="84px"
    >
      <el-form-item label="抵出ID" prop="id">
        <el-input
          placeholder="请输入抵顶物名称"
          v-model="queryParams.id"
        ></el-input>
      </el-form-item>
      <el-form-item label="抵顶物名称" prop="name">
        <el-input
          placeholder="请输入抵顶物名称"
          v-model="queryParams.name"
        ></el-input>
      </el-form-item>
      <el-form-item label="创建时间" prop="repairTime" clearable>
        <el-date-picker
          v-model="times"
          placeholder="请选择"
          type="datetimerange"
          range-separator="~"
          style="width: 340px"
          value-format="yyyy-MM-dd HH:mm:ss"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="业务类型" prop="bizType">
        <el-select v-model="queryParams.bizType" style="width: 100%">
          <el-option label="合同约定" :value="0">合同约定</el-option>
          <el-option label="非合同约定" :value="1">非合同约定</el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="抵入状态" prop="status">
        <el-select v-model="queryParams.status" style="width: 120px" clearable>
          <el-option label="待抵出" :value="0">待抵出</el-option>
          <el-option label="抵出中" :value="1">抵出中</el-option>
          <el-option label="已抵出" :value="2">已抵出</el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['service:repair:add']"
          >新增</el-button
        >
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="repairList" max-height="400">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="抵顶物Id" align="center" prop="id" />
      <el-table-column
        label="抵顶客户"
        :show-overflow-tooltip="true"
        align="center"
        prop="customerName"
        width="160"
      >
        <!-- <template slot-scope="scope">
          <span>{{ formatDate(scope.row.createTime) }}</span>
        </template> -->
      </el-table-column>
      <el-table-column
        label="抵顶物名称"
        :show-overflow-tooltip="true"
        align="center"
        prop="name"
      />
      <el-table-column
        label="单价"
        :show-overflow-tooltip="true"
        align="center"
        prop="price"
      />
      <el-table-column
        label="数量"
        :show-overflow-tooltip="true"
        align="center"
        prop="num"
        width="160px"
      >
      </el-table-column>
      <el-table-column
        label="抵顶总金额"
        :show-overflow-tooltip="true"
        align="center"
        prop="repairShop"
      >
        <template slot-scope="scope">
          <span>{{ (scope.row.price * scope.row.num).toFixed(2) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="抵顶经办人"
        :show-overflow-tooltip="true"
        align="center"
        prop="handleUsername"
      />
      <el-table-column
        label="业务类型"
        :show-overflow-tooltip="true"
        align="center"
        prop="bizType"
        width="160px"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.bizType == '0'">合同约定</span>
          <span v-if="scope.row.bizType == '1'">非合同约定</span>
        </template>
      </el-table-column>
      <el-table-column
        label="抵入状态"
        :show-overflow-tooltip="true"
        align="center"
        prop="statusDesc"
        width="160px"
      >
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        fixed="right"
        width="120px"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['service:repair:edit']"
            >修改</el-button
          >
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['service:repair:remove']"
            >删除</el-button
          > -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  listPutinto,
  // getRepair,
  delRepair,
  // addRepair,
  // updateRepair,
} from "@/api/service/arrive/outer.js";
import { listCars } from "@/api/service/cars";
import { formatDate } from "@/utils";

export default {
  name: "Repair",
  data() {
    return {
      dataList: [],
      carList: [], // che
      repairTypeList: [
        "待抵出",
        "抵出中",
        "已抵出",
        "液压系统",
        "定期保养",
        "换季保养",
      ],
      // 日期范围
      dateRange: [],
      businessTypes: ["合同约定", "非合同约定"],
      // 遮罩层
      loading: true,
      // 选中数组
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车辆维修记录表格数据
      repairList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        name: null,
        createTime: "",
        businessType: "",
        bizType: null,
      },
      times: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        carId: [{ required: true, message: "车辆id不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getList();
  },
  watch: {
    open(n, o) {
      if (n) {
        this.$nextTick(() => {
          this.getCarList();
        });
      }
    },
  },
  methods: {
    handleAdd() {
      this.$router.push({
        path: `/arrive/outerAction`,
      });
    },
    handleUpdate(row) {
      this.$router.push({
        path: `/arrive/outerAction?id=${row.id}`,
      });
    },
    handleCarChange(val) {},

    getCarList() {
      listCars({ pageNum: 1, pageSize: 10000 }).then((res) => {
        this.carList = res.rows;
      });
    },
    formatDate,
    getList() {
      this.loading = true;
      listPutinto({
        ...this.queryParams,
        startTime: this.times?.[0],
        endTime: this.times?.[1],
      }).then((response) => {
        console.log(response);
        let { records, total } = response.data;
        this.repairList = records;
        this.total = total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        carId: null,
        carNumber: null,
        carLicensePlate: null,
        repairTime: null,
        repairType: null,
        repairShop: null,
        repairCost: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        repairCostDetail: null,
        invoices: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.times = [];
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("确定要删除吗？")
        .then(function () {
          return delRepair(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
